<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人财务管理系统</title>
    <!-- 引入Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- 引入Font Awesome -->
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <!-- 引入自定义CSS -->
    <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container-fluid">
            <a class="navbar-brand" href="{{ url_for('index') }}">个人财务管理系统</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item">
                        <a class="nav-link active" aria-current="page" href="{{ url_for('index') }}">交易管理</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('reports') }}">财务报表</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('budget') }}">预算管理</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('settings') }}">设置</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container mt-5">
        <!-- 消息提示区域 -->
        {% with messages = get_flashed_messages() %}
            {% if messages %}
                <div class="messages mb-4">
                    {% for message in messages %}
                        <div class="alert alert-success alert-dismissible fade show" role="alert">
                            {{ message }}
                            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                        </div>
                    {% endfor %}
                </div>
            {% endif %}
        {% endwith %}

        <!-- 财务摘要卡片 -->
        <div class="row mb-5">
            <div class="col-lg-4 col-md-6 mb-4">
                <div class="card summary-card income-card">
                    <div class="card-body">
                        <div class="d-flex justify-content-between items-center">
                            <h5 class="card-title">总收入</h5>
                            <i class="fa fa-arrow-circle-up fa-2x text-green"></i>
                        </div>
                        <p class="card-text amount">¥{{ "%.2f"|format(summary.total_income) }}</p>
                    </div>
                </div>
            </div>
            <div class="col-lg-4 col-md-6 mb-4">
                <div class="card summary-card expense-card">
                    <div class="card-body">
                        <div class="d-flex justify-content-between items-center">
                            <h5 class="card-title">总支出</h5>
                            <i class="fa fa-arrow-circle-down fa-2x text-red"></i>
                        </div>
                        <p class="card-text amount">¥{{ "%.2f"|format(summary.total_expense) }}</p>
                    </div>
                </div>
            </div>
            <div class="col-lg-4 col-md-6 mb-4">
                <div class="card summary-card balance-card">
                    <div class="card-body">
                        <div class="d-flex justify-content-between items-center">
                            <h5 class="card-title">结余</h5>
                            <i class="fa fa-balance-scale fa-2x text-blue"></i>
                        </div>
                        <p class="card-text amount {% if summary.balance < 0 %}text-red{% endif %}">¥{{ "%.2f"|format(summary.balance) }}</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- 操作按钮和筛选区域 -->
        <div class="row mb-4">
            <div class="col-md-6">
                <a href="{{ url_for('add_transaction_route') }}" class="btn btn-success btn-lg">
                    <i class="fa fa-plus"></i> 添加交易
                </a>
                <a href="{{ url_for('export_data') }}" class="btn btn-info btn-lg ml-2">
                    <i class="fa fa-download"></i> 导出数据
                </a>
            </div>
            <div class="col-md-6">
                <form method="GET" action="/" class="filter-form">
                    <div class="row g-2">
                        <div class="col">
                            <select name="type" class="form-select">
                                <option value="">所有类型</option>
                                <option value="income" {% if filters.type == 'income' %}selected{% endif %}>收入</option>
                                <option value="expense" {% if filters.type == 'expense' %}selected{% endif %}>支出</option>
                            </select>
                        </div>
                        <div class="col">
                            <select name="category" class="form-select">
                                <option value="">所有类别</option>
                                {% if filters.type == 'income' %}
                                    {% for category in income_categories %}
                                        <option value="{{ category }}" {% if filters.category == category %}selected{% endif %}>{{ category }}</option>
                                    {% endfor %}
                                {% elif filters.type == 'expense' %}
                                    {% for category in expense_categories %}
                                        <option value="{{ category }}" {% if filters.category == category %}selected{% endif %}>{{ category }}</option>
                                    {% endfor %}
                                {% endif %}
                            </select>
                        </div>
                        <div class="col">
                            <input type="date" name="start_date" class="form-control" value="{{ filters.start_date }}">
                        </div>
                        <div class="col">
                            <input type="date" name="end_date" class="form-control" value="{{ filters.end_date }}">
                        </div>
                        <div class="col">
                            <button type="submit" class="btn btn-primary w-100">筛选</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <!-- 交易列表 -->
        <div class="transaction-list">
            <h2>交易记录</h2>
            {% if transactions %}
                <div class="table-responsive">
                    <table class="table table-hover">
                        <thead class="table-primary">
                            <tr>
                                <th>类型</th>
                                <th>金额</th>
                                <th>类别</th>
                                <th>日期</th>
                                <th>描述</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for transaction in transactions %}
                                <tr class="{% if transaction[1] == 'income' %}table-success{% else %}table-danger{% endif %}">
                                    <td>
                                        {% if transaction[1] == 'income' %}
                                            <span class="badge bg-success">收入</span>
                                        {% else %}
                                            <span class="badge bg-danger">支出</span>
                                        {% endif %}
                                    </td>
                                    <td class="amount">¥{{ "%.2f"|format(transaction[2]) }}</td>
                                    <td>{{ transaction[3] }}</td>
                                    <td>{{ transaction[4] }}</td>
                                    <td>{{ transaction[5] or '-' }}</td>
                                    <td>
                                        <a href="{{ url_for('edit_transaction_route', transaction_id=transaction[0]) }}" class="btn btn-primary btn-sm">
                                            <i class="fa fa-edit"></i> 编辑
                                        </a>
                                        <a href="{{ url_for('delete_transaction_route', transaction_id=transaction[0]) }}" class="btn btn-danger btn-sm ml-1" onclick="return confirm('确定要删除这条交易记录吗？')">
                                            <i class="fa fa-trash"></i> 删除
                                        </a>
                                    </td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            {% else %}
                <div class="alert alert-info" role="alert">
                    暂无交易记录
                </div>
            {% endif %}
        </div>
    </div>

    <!-- 引入Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <!-- 引入自定义JS -->
    <script src="{{ url_for('static', filename='js/main.js') }}"></script>
</body>
</html>